use Bak_Moon_JJPS_replication, clear

set more off
estimates clear
macro drop _all
xi i.year i.ccode  
xtset ccode year


**************
*** Macros ***
**************
global year = "_Iyear_*"
global country = "_Iccode_*"
global fdi = "fdigdpUN"
global fail = "archfail"
global ath = "failpr"  



********************
*** Preparations ***
********************

*** Autocracy Sample ***
keep if gwfauto==1

*** Table A3: ATH Equation ***
*** Table A3: ATH Equation ***
*** Table A3: ATH Equation ***
*** Table A3: ATH Equation ***
*** Table A3: ATH Equation ***
xi: probit $fail oilL1 L.lnoecd_oad_constant2005_gdp prio_interwar_dum prio_intrawar_dum coupL1 massL1 ///
	gwfmil archigos_pastleaderfail archdura1 archdura2 archdura3 i.ht_region2 i.ccode
 
lroc, nograph
estat class
predict failpr if e(sample), pr

*** Growth Equation Controls ***
global controlgr = "PWT71_openk priowar bankingcrisis lnpwt81emp PWT81_hc cimmark"

gen fdi_ath = $fdi * $ath

save tempfile, replace



/* Table 1: OLS */
/* Table 1: OLS */
/* Table 1: OLS */
/* Table 1: OLS */
/* Table 1: OLS */
/* Table 1: OLS */

reg growth0 L.$fdi L.$ath L.fdi_ath L.($controlgr)
xtreg growth0 L.$fdi L.$ath L.fdi_ath L.($controlgr), fe 
xi: reg growth0 L.$fdi L.$ath L.fdi_ath L.($controlgr) i.year
xi: xtreg growth0 L.$fdi L.$ath L.fdi_ath L.($controlgr) i.year, fe 
xtregar growth0 L.$fdi L.$ath L.fdi_ath L.($controlgr), fe 
xtreg growth0 L.growth0 L.$fdi L.$ath L.fdi_ath L.($controlgr), fe 
xtreg growth0 L.growth0 L.$fdi L.$ath L.fdi_ath L.($controlgr) 



/* Table 2: Dynamic Panel GMM */
/* Table 2: Dynamic Panel GMM */
/* Table 2: Dynamic Panel GMM */
/* Table 2: Dynamic Panel GMM */
/* Table 2: Dynamic Panel GMM */


xi i.year i.ccode  

*** Model (1) Difference GMM: max-lag instruments
xtabond2 PWT71_grgdpch L.PWT71_lgdppc L.$fdi L.$ath L.fdi_ath L.($controlgr) $year, ///
	gmm(L.PWT71_lgdppc, lag(1 .)) ///
	gmm(L.$fdi L.$ath L.fdi_ath, lag(2 .)) ///
	iv($year L.priowar) small robust nolevel


*** Model (2) Difference GMM: optimal-lag instruments
/* Table A4 */
/* Table A4 */
/* Table A4 */
/* Table A4 */
/* Table A4 */
forvalues i = 3(1)20 {
xtabond2 PWT71_grgdpch L.PWT71_lgdppc L.$fdi L.$ath L.fdi_ath L.($controlgr) $year, ///
	gmm(L.PWT71_lgdppc, lag(1 `i')) ///
	gmm(L.$fdi L.$ath L.fdi_ath, lag(2 `i')) ///
	iv($year L.priowar) small robust nolevel
predict res_`i' if e(sample), re 
gen res2_`i' = res_`i'^2 if e(sample)
egen mse_`i' = mean(res2_`i') if e(sample) 	 
sum mse_`i'
scalar rmse_`i' = sqrt(r(mean))
}
scalar list	  




		 
xtabond2 PWT71_grgdpch L.PWT71_lgdppc L.$fdi L.$ath L.fdi_ath L.($controlgr) $year, ///
	gmm(L.PWT71_lgdppc, lag(1 14)) ///
	gmm(L.$fdi L.$ath L.fdi_ath, lag(2 15)) ///
	iv($year L.priowar) small robust nolevel


*** Model (3) Difference GMM: collapsed instruments
xtabond2 PWT71_grgdpch L.PWT71_lgdppc L.$fdi L.$ath L.fdi_ath L.($controlgr) $year, ///
	gmm(L.PWT71_lgdppc, lag(1 .) collapse) ///
	gmm(L.$fdi L.$ath L.fdi_ath, lag(2 .) collapse) ///
	iv($year L.priowar) small robust nolevel

	
*** Model (4) Difference GMM: collapsed pca instruments
xtabond2 PWT71_grgdpch L.PWT71_lgdppc L.$fdi L.$ath L.fdi_ath L.($controlgr) $year, ///
	gmm(L.PWT71_lgdppc, lag(1 .) collapse) ///
	gmm(L.$fdi L.$ath L.fdi_ath, lag(2 .) collapse) ///
	iv($year L.($controlgr)) small robust nolevel pca

	
*** Model (5) System GMM
xtabond2 PWT71_grgdpch L.PWT71_lgdppc L.$fdi L.$ath L.fdi_ath L.($controlgr) $year, ///
	gmm(L.PWT71_lgdppc, lag(1 .)) ///
	gmm(L.$fdi L.$ath L.fdi_ath, lag(2 .)) ///
	iv($year L.priowar) small robust  	

	


******************
*** Prediction ***	
******************

/* OLS: Post-estimation analysis */	
xi: xtreg growth0 L.growth0 L.$fdi L.$ath L.fdi_ath L.($controlgr) i.year, fe 

mat dv_ols = J(100,3,0)  
forvalues i = 1(1)100 {
	quietly lincom L.$fdi + (L.fdi_ath*`i'/100)
	scalar k = `i'
	mat dv_ols[k,1] = r(estimate)	
	mat dv_ols[k,2] = r(estimate) - (1.96*r(se))							
	mat dv_ols[k,3] = r(estimate) + (1.96*r(se))							
}
capture drop dv_ols*
svmat double dv_ols
capture drop ath
gen ath = _n/100
gen dv_ols2_abs = abs(dv_ols2)
egen dv_ols2_min = min(dv_ols2_abs)
sum ath if dv_ols2_abs == dv_ols2_min
sum L.$ath if e(sample), detail
dis r(p95)
count if e(sample)
count if e(sample) & failpr<=0.07
save dv_ols, replace
twoway (line dv_ols1 dv_ols2 dv_ols3 ath if ath<.47, ///
		yline(0, lstyle(foreground)) ///
		clpattern(solid dash dash) yaxis(1)) ///
		(hist $ath if e(sample) & $ath<.4, fcolor(none) lcolor(gs12) bin(100) yaxis(2)), ///
		legend(off) ///
		ytitle("Effect of FDI", size(3)) title("", size(3)) scheme(s1mono)  ///
		xtitle("Pr(Failure)") saving(OLS, replace) 

	
	
/* Dynamic Panel system GMM: Post-estimation analysis */
xtabond2 PWT71_grgdpch L.PWT71_lgdppc L.$fdi L.$ath L.fdi_ath L.($controlgr) $year, ///
	gmm(L.PWT71_lgdppc, lag(1 .) collapse) ///
	gmm(L.$fdi L.$ath L.fdi_ath, lag(2 .) collapse) ///
	iv($year L.($controlgr)) small robust nolevel pca

mat dv_dpd = J(100,3,0)  
forvalues i = 1(1)100 {
	quietly lincom L.$fdi + (L.fdi_ath*`i'/100)
	scalar k = `i'
	mat dv_dpd[k,1] = r(estimate)	
	mat dv_dpd[k,2] = r(estimate) - (1.96*r(se))							
	mat dv_dpd[k,3] = r(estimate) + (1.96*r(se))							
}
capture drop dv_dpd*
svmat double dv_dpd
capture drop ath
gen ath = _n/100
gen dv_dpd2_abs = abs(dv_dpd2)
egen dv_dpd2_min = min(dv_dpd2_abs)
sum ath if dv_dpd2_abs == dv_dpd2_min
sum L.$ath if e(sample), detail
dis r(p95)
count if e(sample)
count if e(sample) & failpr<=0.11
save dv_dpd, replace
twoway (line dv_dpd1 dv_dpd2 dv_dpd3 ath if ath<.41, ///
		yline(0, lstyle(foreground)) ///
		clpattern(solid dash dash) yaxis(1)) ///
		(hist $ath if e(sample) & $ath<.4, fcolor(none) lcolor(gs12) bin(100) yaxis(2)), ///
		legend(off) ///
		ytitle("Effect of FDI", size(3)) title("", size(3)) scheme(s1mono)  ///
		xtitle("Pr(Failure)") saving(Dynamic-Panel-System-GMM, replace) 

******************************
*** This graph is Figure 2 ***
*** This graph is Figure 2 ***
*** This graph is Figure 2 ***
******************************





**********************************************
*** Table 3: Pr(fail) ---> Property Rights *** 
*** Table 3: Pr(fail) ---> Property Rights *** 
*** Table 3: Pr(fail) ---> Property Rights *** 
*** Table 3: Pr(fail) ---> Property Rights *** 
*** Table 3: Pr(fail) ---> Property Rights *** 
*** Table 3: Pr(fail) ---> Property Rights *** 
**********************************************

use tempfile, clear

reg cimmark failpr lngdppcL1 L.cimmark $country, robust
xtscc cimmark failpr lngdppcL1, fe lag(1)
reg icrg failpr lngdppcL1 L.icrg $country, robust
xtscc icrg failpr lngdppcL1, fe lag(1)




***************
*** Table 4 *** 
*** Table 4 *** 
*** Table 4 *** 
*** Table 4 *** 
*** Table 4 *** 
*** Table 4 *** 
***************

use Bak_Moon_JJPS_replication, clear
tsset ccode year
xi i.year i.ccode 

gen fdilproperty = fdigdpUN*cimmark

* cimmark
xtabond2 PWT71_grgdpch L.PWT71_lgdppc L.$fdi L.cimmark L.fdilproperty ///
	L.(PWT71_openk priowar bankingcrisis lnpwt81emp PWT81_hc) $year if gwfdem==1, ///
	gmm(L.PWT71_lgdppc, lag(1 .) collapse) ///
	gmm(L.$fdi L.cimmark L.fdilproperty, lag(2 .) collapse) ///
	iv($year L.(PWT71_openk priowar bankingcrisis lnpwt81emp PWT81_hc)) small robust nolevel pca	

use tempfile, clear
tsset ccode year
xi i.year i.ccode 
gen fdilproperty = fdigdpUN*cimmark

xtabond2 PWT71_grgdpch L.PWT71_lgdppc L.$fdi L.cimmark L.fdilproperty ///
	L.(PWT71_openk priowar bankingcrisis lnpwt81emp PWT81_hc) $year if gwfauto==1, ///
	gmm(L.PWT71_lgdppc, lag(1 .) collapse) ///
	gmm(L.$fdi L.cimmark L.fdilproperty, lag(2 .) collapse) ///
	iv($year L.(PWT71_openk priowar bankingcrisis lnpwt81emp PWT81_hc)) small robust nolevel pca	

xtabond2 PWT71_grgdpch L.PWT71_lgdppc L.$fdi L.cimmark L.fdilproperty L.$ath L.fdi_ath ///
	L.(PWT71_openk priowar bankingcrisis lnpwt81emp PWT81_hc) $year if gwfauto==1, ///
	gmm(L.PWT71_lgdppc, lag(1 .) collapse) ///
	gmm(L.$fdi L.cimmark L.fdilproperty L.$ath L.fdi_ath, lag(2 .) collapse) ///
	iv($year L.(PWT71_openk priowar bankingcrisis lnpwt81emp PWT81_hc)) small robust nolevel pca	
	
	

	
	
	

****************
*** Table A2 ***
*** Table A2 ***
*** Table A2 ***
*** Table A2 ***
*** Table A2 ***
*** Table A2 ***
****************

use tempfile, clear

reg growth0 L.$fdi L.$ath L.fdi_ath L.PWT71_lgdppc L.($controlgr)
xtreg growth0 L.$fdi L.$ath L.fdi_ath L.PWT71_lgdppc L.($controlgr), fe
xi: xtreg growth0 L.$fdi L.$ath L.fdi_ath L.PWT71_lgdppc L.($controlgr) i.year
xi: xtreg growth0 L.$fdi L.$ath L.fdi_ath L.PWT71_lgdppc L.($controlgr) i.year, fe 
xtregar growth0 L.$fdi L.$ath L.fdi_ath L.PWT71_lgdppc L.($controlgr), fe 
xtreg growth0 L.growth0 L.$fdi L.$ath L.PWT71_lgdppc L.fdi_ath L.($controlgr), fe 


	

****************
*** Table A5 ***
*** Table A5 ***
*** Table A5 ***
*** Table A5 ***
*** Table A5 ***
*** Table A5 ***
****************

use tempfile, clear

set more off
estimates clear
macro drop _all
xi i.year i.ccode  
xtset ccode year

global year = "_Iyear_*"
global country = "_Iccode_*"
global fdi = "fdigdpUN"

global fail = "archfail"
global ath = "failpr" 

keep if gwfauto==1

global controlgr = "PWT71_openk priowar bankingcrisis lnpwt81emp PWT81_hc cimmark"

capture program drop myboot
prog myboot, eclass
	xi: probit $fail oilL1 L.lnoecd_oad_constant2005_gdp prio_interwar_dum prio_intrawar_dum coupL1 massL1 ///
	gwfmil archigos_pastleaderfail archdura1 archdura2 archdura3 i.ht_region2 i.ccode 
	capture drop failpr
	predict failpr if e(sample), pr
	capture drop fdi_ath
	gen fdi_ath = $fdi * failpr
	xi: xtreg growth0 L.$fdi L.failpr L.fdi_ath L.($controlgr), fe 
	tempvar touse 
	tempname b V
	matrix `b'=e(b) 
	matrix `V'=e(V) 
	quietly gen byte `touse' = e(sample) 
	ereturn post `b' `V', esample(`touse') 
	drop failpr fdi_ath
end

bootstrap _b, cluster(ccode) idcluster(newid) group(ccode) saving(ols_ath_boot, replace) ties reps(1000) seed(1124): myboot, iterate(10) reject(e(converged)==0)





****************
*** Table A6 ***
*** Table A6 ***
*** Table A6 ***
*** Table A6 ***
*** Table A6 ***
*** Table A6 ***
****************

use tempfile, clear
global controlgr = "PWT71_openk priowar bankingcrisis lnpwt81emp PWT81_hc cimmark cumbit_ratify"

/* OLS */
reg growth0 L.$fdi L.$ath L.fdi_ath L.($controlgr)
xtreg growth0 L.$fdi L.$ath L.fdi_ath L.($controlgr), fe 
xi: reg growth0 L.$fdi L.$ath L.fdi_ath L.($controlgr) i.year
xi: xtreg growth0 L.$fdi L.$ath L.fdi_ath L.($controlgr) i.year, fe 
xtregar growth0 L.$fdi L.$ath L.fdi_ath L.($controlgr), fe 
xtreg growth0 L.growth0 L.$fdi L.$ath L.fdi_ath L.($controlgr), fe 
xtreg growth0 L.growth0 L.$fdi L.$ath L.fdi_ath L.($controlgr) 





